package com.superme.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.Resource;

@Slf4j
@Configuration
public class MyBeans {

    @Resource
    private RedisConfig redisConfig;

    /**
     * 连接池
     */
    @Bean
    public JedisPool jedisPool() {
        log.info("JedisPool注入成功！！");
        log.info("redis地址：{}:{}", redisConfig.getHost(), redisConfig.getPort());
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
        return new JedisPool(jedisPoolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout(), redisConfig.getPassword());
    }

    //使用jedisPool注册jedis实例
    @Bean
    public Jedis getJedis() {
        if (jedisPool() != null) {
            return jedisPool().getResource();
        } else {
            return null;
        }
    }
}
